# GtkTrayIcon.py
# Copyright (C) otacon 2010 <orfeo18@gmail.com>
# 
# AdSense-Monitor is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# AdSense-Monitor is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import threading
try:
    import winsound
    windows = True
except:
    windows = False
    
class SoundNotify() :

    def __init__(self, sound_path = None):
        if sound_path:
            self.set_sound(sound_path)

    def play(self):
        player = Player(self._sound_path)
        player.start()

    def set_sound(self, sound_path):
        if sound_path:
            if os.path.exists(sound_path):
                self._sound_path = sound_path
        else:
            self._sound_path = None

class Player(threading.Thread):

    def __init__(self, sound_path):
        threading.Thread.__init__(self)
        self.sound = sound_path

    def run(self):
        if self.sound:
            if windows:
                winsound.PlaySound(self.sound,winsound.SND_FILENAME)
            else:
                os.system('aplay -q ' + self.sound)
